package com.hiray.demos.entry

import android.os.Build
import android.os.Bundle
import android.security.keystore.KeyGenParameterSpec
import android.security.keystore.KeyProperties
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import android.util.Log
import com.hiray.demos.R
import java.math.BigInteger
import java.security.KeyPairGenerator
import java.util.*
import javax.security.auth.x500.X500Principal

class FingerPrintUnlockActivity : AppCompatActivity() {
    val TAG = "FingerPrintUnlock"

    var alias = "mAlias"
    @RequiresApi(Build.VERSION_CODES.M)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_finger_print_unlock)

        val canlendar = Calendar.getInstance()
        val start = canlendar.time
        canlendar.add(Calendar.YEAR, 1)
        val end = canlendar.time
        var keyGenParameterSpec = KeyGenParameterSpec.Builder(alias, KeyProperties.PURPOSE_SIGN)
                .setCertificateSubject(X500Principal("CN=$alias"))
                .setCertificateSerialNumber(BigInteger(1234.toString()))
                .setDigests(KeyProperties.DIGEST_SHA256)
                .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
                .setSignaturePaddings(KeyProperties.SIGNATURE_PADDING_RSA_PKCS1)
                .setCertificateNotBefore(start)
                .setCertificateNotAfter(end)
                .setBlockModes(KeyProperties.BLOCK_MODE_GCM, KeyProperties.BLOCK_MODE_CTR, KeyProperties.BLOCK_MODE_CBC,
                        KeyProperties.BLOCK_MODE_ECB)
                .build()

        var keyPairGenerator = KeyPairGenerator.getInstance("RSA", "AndroidKeyStore")
        keyPairGenerator.initialize(keyGenParameterSpec)

        var keyPair = keyPairGenerator.generateKeyPair()
        Log.i(TAG, ": privateKey:${keyPair.private.toString()} publicKey:${keyPair.public.toString()}")

    }

}
